package hhm.android.library.weightHeightPicker.footPicker;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import hhm.android.library.datePicker.view.WheelView;
import hhm.android.library.utils.GetStringListener;
import hhm.android.library.weightHeightPicker.heightPicker.HeightParams;

public class FootPickerView extends LinearLayout {

    final FootPick mDatePick = new FootPick();
    private GetStringListener mOnChangeListener;

    public FootPickerView(Context context) {
        super(context);
    }

    public FootPickerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FootPickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setOnChangeListener(GetStringListener listener) {
        this.mOnChangeListener = listener;
    }

    public void show(double initData, @NonNull FootParams params, int selectColor) {
        setInitData(initData);

        if (params.types == null) {
            return;
        }
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;
        for (int type : params.types) {

            WheelView wheelView = new WheelView(getContext());
            final BaseFootAdapter datePickAdapter;

            switch (type) {
                case HeightParams.TYPE_CM:
                    wheelView.setCyclic(false);
                    datePickAdapter = new CMFootAdapter(params, mDatePick, selectColor);
                    wheelView.setAdapter(datePickAdapter);
                    wheelView.setCurrentItem(datePickAdapter.getCurrentIndex());
                    layoutParams.weight = 2;
                    wheelView.addChangingListener((wheel, oldValue, newValue) -> {
                        mDatePick.cm = datePickAdapter.getValue(newValue);
                        FootPickerView.this.onChanged();
                    });
                    break;
                case HeightParams.TYPE_MM:
                    wheelView.setCyclic(true);
                    datePickAdapter = new MMFootAdapter(params, mDatePick, selectColor);
                    wheelView.setAdapter(datePickAdapter);
                    wheelView.setCurrentItem(datePickAdapter.getCurrentIndex());
                    layoutParams.weight = 2;
                    wheelView.addChangingListener((wheel, oldValue, newValue) -> {
                        mDatePick.mm = datePickAdapter.getValue(newValue);
                        FootPickerView.this.onChanged();
                    });
                    break;
            }

            addView(wheelView, layoutParams);
        }
    }

    private void onChanged() {
        if (mOnChangeListener != null) {
            mOnChangeListener.getString(getChildHeight());
        }
    }

    public String getChildHeight() {
        int cm = mDatePick.cm;
        int mm = mDatePick.mm;

        return cm + "." + mm;
    }

    public void setInitData(double initData) {
        if (initData == -1) {
            mDatePick.setData(0, 0);
        } else {
            String data = String.valueOf(initData);
            int big;
            int small;
            if (data.contains(".")) {
                String[] split = data.split("\\.");
                big = Integer.parseInt(split[0]);
                small = Integer.parseInt(split[1]);
            } else {
                big = Integer.parseInt(data);
                small = 0;
            }
            mDatePick.setData(big, small);
        }
    }
}
